
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class pdf2png {

    public static void main(String[] args) {
        String pdfFilePath = "C:\\Users\\tlcb\\Documents\\WeChat Files\\wxid_25qf7q4iry5t21\\FileStorage\\File\\2024-06\\v3.2.0.1\\Linux_GCC_64\\文档\\数字人民币.pdf";
        String outputImagePath = "C:\\Users\\tlcb\\Documents\\WeChat Files\\wxid_25qf7q4iry5t21\\FileStorage\\File\\2024-06\\v3.2.0.1\\Linux_GCC_64\\文档\\数字人民币image.png";
//        try {
//            PDDocument document = PDDocument.load(new File(pdfFilePath));
//            PDFRenderer pdfRenderer = new PDFRenderer(document);
//            int pageCount = document.getNumberOfPages();
//
//            // 渲染每个PDF页面为图像
//            BufferedImage[] images = new BufferedImage[pageCount];
//            int totalHeight = 0;
//            int maxWidth = 0;
//
//            for (int i = 0; i < pageCount; i++) {
//                images[i] = pdfRenderer.renderImageWithDPI(i, 300); // 使用300 DPI渲染图像
//                totalHeight += images[i].getHeight();
//                if (images[i].getWidth() > maxWidth) {
//                    maxWidth = images[i].getWidth();
//                }
//            }
//
//            // 创建一个长图像
//            BufferedImage longImage = new BufferedImage(maxWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
//            Graphics2D g2d = longImage.createGraphics();
//
//            int currentHeight = 0;
//            for (BufferedImage image : images) {
//                g2d.drawImage(image, 0, currentHeight, null);
//                currentHeight += image.getHeight();
//            }
//            g2d.dispose();
//
//            // 保存长图像
//            ImageIO.write(longImage, "png", new File(outputImagePath));
//            document.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        // 使用 try-with-resources 语句来自动关闭资源
        //2
        try (PDDocument document = PDDocument.load(new File(pdfFilePath))) {
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            int pageCount = document.getNumberOfPages();

            // 渲染每个PDF页面为图像
            BufferedImage[] images = new BufferedImage[pageCount];
            int totalHeight = 0;
            int maxWidth = 0;

            for (int i = 0; i < pageCount; i++) {
                images[i] = pdfRenderer.renderImageWithDPI(i, 300); // 使用300 DPI渲染图像
                totalHeight += images[i].getHeight();
                if (images[i].getWidth() > maxWidth) {
                    maxWidth = images[i].getWidth();
                }
            }

            // 创建一个长图像
            BufferedImage longImage = new BufferedImage(maxWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = longImage.createGraphics();

            int currentHeight = 0;
            for (BufferedImage image : images) {
                g2d.drawImage(image, 0, currentHeight, null);
                currentHeight += image.getHeight();
            }
            g2d.dispose();

            // 保存长图像
            ImageIO.write(longImage, "png", new File(outputImagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //2 closed

    }
}
